import axios from 'axios'
import { createHashHistory } from 'history'

const history = createHashHistory();
const request = axios.create({
	baseURL:"/api/v1",
	timeOut: 18000
})

// 拦截
request.interceptors.request.use(function (config) {
  // 请求头中加token
  // 获取 token
  const token = localStorage.getItem('token');
  if(token){
    config.headers['access_token'] = token
  }
    return config;
  }, function (error) {
    return Promise.reject(error);
  });


request.interceptors.response.use(function (response) {
  // token过期处理 code 401  402(么有传token)
    if(response.data.code === '401' || response.data.code==='402'){
      // 重定向到 登录页
      history.push("/login")
      history.go(0)
    }
    return response;
  }, function (error) {
    return Promise.reject(error);
 });
 
 export default request